package xmu.zfw.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class CheckNull {

    public static Object chekNull(Object object) throws Exception {
        Field[] fields = object.getClass().getDeclaredFields();
        for(int i = 0; i < fields.length; i++){
            String name = fields[i].getName();
            name = name.substring(0,1).toUpperCase()+name.substring(1);
            String type = fields[i].getGenericType().toString();
            Method getter = object.getClass().getMethod("get" + name);

            Object field = getter.invoke(object);
            if(field != null){
                continue;
            }
            if(type.equals("class java.lang.String")){
                Method setter = object.getClass().getMethod("set" + name, new Class[] {String.class});
                setter.invoke(object, new Object[] {new String("")});
            }
        }
        return object;
    }
}
